<?php
namespace Admin\Controller;
use Think\Controller;
class ProdectController extends Controller {
	public function __construct(){
		parent::__construct();
		if(!$_SESSION['admin_is_login']){
			$this->error('没有登录',U('admin/auth/logout'));
		}
	}	
    public function prodect(){
    	$prodect_model=M('prodect'); // 实例化prodect对象
		$count=$prodect_model->count();// 查询满足要求的总记录数
		$Page=new \Org\Util\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show= $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$prodects = $prodect_model->order('prodect.id desc')->field('*,prodect.id as id')->join('prodect_catalog ON prodect.catalog_id = prodect_catalog.id','left')->limit($Page->firstRow.','.$Page->listRows)->select();
		$this->assign('prodects',$prodects);// 赋值数据集
		$this->assign('page',$show);// 赋值分页输出
		$this->display(); // 输出模板
    }
     public function del($id){
        M('prodect')->where('id='.$id)->delete();
     }
     public function add(){
     	$level1=M('prodect_catalog')->where('parent_id=0')->select();
     	$this->assign('level1',$level1);
     	$this->display('Prodect_form');
     }
     public function getcatalog(){
     	$id=I('get.id');
     	$data=M('prodect_catalog')->where('parent_id='.$id)->select();
     	$this->ajaxReturn($data);
     }
     public function del_all(){
     	 $ids=I('post.ids');
        M('prodect')->delete(implode(',', $ids));
        echo 'ok';
     }
     public function do_add(){
     	$rules = array(
			array('title','require','标题必须！'),
			array('desc','require','描述必须！'),
			array('price','require','价格必须！'),
			array('stock','require','库存必须！'),
			array('img','require','照片必须！'),
			array('catalog_id','require','分类必须！'),
			array('content','require','内容必须！'),
		);
		$prodect_model = M("prodect"); // 实例化User对象
		if (!$prodect_model->validate($rules)->create()){
		     // 如果创建失败 表示验证没有通过 输出错误提示信息
		     $this->error($prodect_model->getError());
		}else{
			M('prodect')->add(array(
					'title'=>I('post.title'),
					'desc'=>I('post.desc'),
					'price'=>I('post.price'),
					'stock'=>I('post.stock'),
					'img'=>I('post.img'),
					'catalog_id'=>I('post.catalog_id'),
					"content"=>I('post.content')
				));
			$this->success('添加成功',U('admin/prodect/prodect'));
		}
     }
     public function edit($id){
     	$prodect=M("prodect")->where('id='.$id)->find();
     	$level3=$prodect['catalog_id'];
     	$level3_catalog=M('prodect_catalog')->where('id='.$level3)->find();
     	$level2=$level3_catalog['parent_id'];
     	$level2_catalog=M('prodect_catalog')->where('id='.$level2)->find();
     	$level1=$level2_catalog['parent_id'];
     	$this->assign('level1',$level1);
     	$this->assign('level2',$level2);
     	$this->assign('level3',$level3);
     	$this->assign('prodect',$prodect);
   		$level1=M('prodect_catalog')->where('parent_id=0')->select();
     	$this->assign('level1data',$level1);
     	$this->display();
     }
     public function do_edit($id){
     	$rules = array(
			array('title','require','标题必须！'),
			array('desc','require','描述必须！'),
			array('price','require','价格必须！'),
			array('stock','require','库存必须！'),
			array('img','require','照片必须！'),
			array('catalog_id','require','分类必须！'),
			array('content','require','内容必须！'),
		);
		$prodect_model = M("prodect"); // 实例化User对象
		if (!$prodect_model->validate($rules)->create()){
		     // 如果创建失败 表示验证没有通过 输出错误提示信息
		     $this->error($prodect_model->getError());
		}else{
			M('prodect')->where('id='.$id)->save(array(
					'title'=>I('post.title'),
					'desc'=>I('post.desc'),
					'price'=>I('post.price'),
					'stock'=>I('post.stock'),
					'img'=>I('post.img'),
					'catalog_id'=>I('post.catalog_id'),
					"content"=>I('post.content')
				));
			$this->success('编辑成功',U('admin/prodect/prodect'));
		}
     }
}